/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package technotheist.geo.graph;

import com.jme3.math.Vector3f;

/**
 *
 * @author Mitch
 */
public class GeoUtil {

    static final float BND = 1.01f;

    public static int SolveOctal(Vector3f vec) {
        return SolveOctal(vec.x, vec.y, vec.z);
    }
    
    public static int SolveOctal(float x, float y, float z) {
        if (x < -BND || x > BND || y < -BND || y > BND || z < -BND || z > BND) {
            throw new IllegalArgumentException("Parameters x,y,z must be between -1.0f and +1.0f");
        }
        float cx = 0;
        float cy = 0;
        float cz = 0;
        float h = 0.5f;
        int oct = 0;
        boolean rx, ry, rz;
        for (int i = 9; i >= 0; i--) {
            rx = x > cx;
            ry = y > cy;
            rz = z > cz;
            oct |= ((rx ? 4 : 0) | (ry ? 2 : 0) | (rz ? 1 : 0)) << (i * 3);
            cx += rx ? +h : -h;
            cy += ry ? +h : -h;
            cz += rz ? +h : -h;
        }
        return oct;
    }
}
